<?php

/**
 * 用户模块
 *
 * Created by PhpStorm.
 * User: evan
 * Date: 2016/12/13
 * Time: 上午11:31
 */
class User extends MY_M_Controller
{
    /**
     * 用户个人信息
     */
    public function profile()
    {
    	$uid = (int)$this->input->get_post('uid');


        if(!$uid)
        {
        	show_error('参数错误！', '500', '提示');
        }


        $this->load->service('user/service_user_profile');
        $data = $this->service_user_profile->who($uid, $uid);

        //p($data);
        $this->load->view('m/user/profile', $data);
    }
    
    
    /**
     * ajax用户个人信息
     */
    public function info()
    {
    	$uid = (int)$this->input->get_post('uid');
    
    
    	if(!$uid)
    	{
    		$this->json_output(array(), 10002);
    		exit;
    	}
    
    
    	$this->load->model('user/Model_user', 'u');
        $user_item = $this->u->getOne(array('uid'=>$uid, 'uid, nickname'));
    
    	$this->json_output($user_item);
    }

    /**
     * 代理邀请
     */
    public function agent_invite()
    {
    	$sid = (int)$this->input->get_post('sid');
    	$uid = (int)$this->input->get_post('uid');
    	 
    	if(!$sid && !$uid)
    	{
    		show_error('参数错误！', '500', '提示');
    	}
    	
    	if($sid)
    	{
	    	$this->load->model('agent/Model_agent_staff', 'staff');
	    	$staff_info = $this->staff->getById($sid);
	    	
	    	$this->load->model('agent/Model_agent', 'agent');
	    	$agent_info = $this->agent->getById($staff_info['aid']);
	    	
	    	$uid = $agent_info['uid'];
	    	$nickname = $staff_info['realname'].'('.$agent_info['company'].')';
    	}
    	
    	$this->load->model('user/Model_user', 'u');
    	$user_info = $this->u->getById($uid);
    	
    	$nickname = $user_info['nickname'];
    
		$data = array('data'=>array('nickname'=>$nickname, 'invite_code'=>$user_info['recommend_code'], 'sid'=>$sid));
    
    	$this->load->view('m/user/agent_invite', $data);
    }

    /**
     * 邀请好友
     */
    public function invite()
    {
    	$uid = (int)$this->input->get_post('uid');
    	
    	if(!$uid)
    	{
    		show_error('参数错误！', '500', '提示');
    	}


    	$this->load->service('user/service_user_helper');
    	$data = $this->service_user_helper->invite($uid);


    	//获取微信分享所需要的参数
        $this->load->library('wxjs/wxjs');
        $shareparam=$this->wxjs->getParams();

       $data=array_merge($data,$shareparam);



        $this->load->view('m/user/invite', $data);

    }
    
    /**
     * 用户协议
     */
    public function user_agreement()
    {
    	$this->load->view('m/user/user_agreement');
    }


    /**
     * 领取奖励
     */
    public function receive_reward()
    {
        $mobile      = trim($this->input->get_post('mobile'));
        $code        = trim($this->input->get_post('code'));
        $invite_code = trim($this->input->get_post('invite_code'));
        $sid         = trim($this->input->get_post('sid'));


        $this->load->service('user/service_user_receive_reward');
        $data = $this->service_user_receive_reward->receive_reward($mobile, $code, $invite_code, $sid);

        echo json_encode($data);
        exit;
    }
}